﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MIMSDomain.Entities;
using MIMSDomain.Abstract;
using Moq;

namespace MIMSUnitTests.Domain
{
    [TestClass]
    public class EFReligionRepositoryTests
    {
        public readonly IRepository<Religion> MockReligionRepository;

         public EFReligionRepositoryTests()
        {
            //Arrange
            var _Religions = new List<Religion>{
                new Religion{Id=1, Name="SCIENTOLOGY" },
                new Religion{Id=2, Name="CDE" },
                new Religion{Id=3, Name="EDF"},
                new Religion{Id=4, Name="GHI"}
            };

            //Mock the  Repository
            var target = new Mock<IRepository<Religion>>();
            target.Setup(p => p.All).Returns(_Religions.AsQueryable());

            target.Setup(p => p.GetById(It.IsAny<int>())).Returns((int i) =>
                _Religions.Where(p => p.Id == i).Single());
            target.Setup(p => p.Save(It.IsAny<Religion>())).Returns((Religion obj) =>
                {
                    if (obj.Id.Equals(default(int)))
                    {
                        obj.Id = _Religions.Count() + 1;
                        _Religions.Add(obj);
                    }
                    else
                    {
                        var original = _Religions.Where(p => p.Id == obj.Id).Single();
                        if (original == null)
                        {
                            return false;
                        }
                        original.Name = obj.Name;
                    }
                    return true;
                });
            target.Setup(p => p.Delete(It.IsAny<Religion>())).Returns((Religion obj) =>
                {

                    if (_Religions.Contains(obj))
                    {
                        _Religions.Remove(obj);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                });
            
            MockReligionRepository = target.Object;
        }
        
        [TestMethod]
        public void CanSave()
        {
            //Arrange
            var count = MockReligionRepository.All.Count();
            //Assert
            Assert.AreEqual(4, count); //Confirm  count
            //Act
            var expected = MockReligionRepository.Save(new Religion { Name = "ROC" });

            //Assert
            count = MockReligionRepository.All.Count();
            Assert.AreEqual(5, count); //Confirm  count after saving

        }

        [TestMethod]
        public void CannotAddDuplicate()
        {
            //Arrange
            var count = MockReligionRepository.All.Count();
            //Assert
            Assert.AreEqual(4, count); //Confirm  count
            //Act
            var expected = MockReligionRepository.Save(new Religion { Name = "ROC" });

            //Assert
            count = MockReligionRepository.All.Count();
            Assert.AreEqual(5, count); //Confirm  count after saving
        }

        [TestMethod]
        public void CanGetById()
        {
            //Act
            var expected = MockReligionRepository.GetById(3);
            //Assert
            Assert.IsNotNull(expected);
            Assert.IsInstanceOfType(expected, typeof(Religion));
            Assert.AreEqual(expected.Name, "EDF");
        }

        [TestMethod]
        public void CanGetAll()
        {
            //Act
            var expected = MockReligionRepository.All;
            //Assert
            Assert.IsNotNull(expected);
            Assert.IsInstanceOfType(expected, typeof(IQueryable<Religion>));
            Assert.AreEqual(expected.Count(), 4);
        }

        [TestMethod]
        public void CanDelete()
        {
            //Arrange
            var count = MockReligionRepository.All.Count();
            Assert.AreEqual(4, count);
            var objAllergy = MockReligionRepository.GetById(3);
            Assert.IsInstanceOfType(objAllergy, typeof(Religion));
            //Act
            MockReligionRepository.Delete(objAllergy);
            count = MockReligionRepository.All.Count();
            //Assert
            Assert.AreEqual(3, count);
        }
       
    }
}
